package com.buss.product.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.buss.core.dic.RecordStatus;
import com.buss.core.exception.ServiceException;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.product.dao.GoodsDao;
import com.buss.product.model.Goods;
import com.buss.product.model.GoodsDetail;
import com.buss.product.model.GoodsProp;
import com.buss.product.service.GoodsDetailService;
import com.buss.product.service.GoodsPropService;
import com.buss.product.service.GoodsService;

@Service
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	private GoodsDao goodsDao;
	
	@Autowired
	private GoodsPropService goodsPropService;
	
	@Autowired
	private GoodsDetailService detailService;
	
	@Override
	public Goods saveGoods(Goods goods) throws ServiceException{
		if(goodsDao.loadGoods(goods.getCategory().getCompanyId(), goods.getCode()) != null){
			throw new ServiceException("商品编码已经存在，请核实!");
		}
		Goods tmpGoods = goodsDao.saveGoods(goods);
		for(GoodsProp prop : goods.getProps()){
			prop.setGoods(tmpGoods);
			goodsPropService.saveGoodsProp(prop);
		}
		
		return tmpGoods;
	}

	@Override
	public void editGoods(Goods goods) {	
		List<GoodsDetail> details = detailService.queryGoodsDetail(goods.getId()).getData();
		List<Long> ids = new ArrayList<Long>();
		for(GoodsDetail detail : goods.getDetails()){
			for(GoodsDetail tmpDetail : details){
				String[] detailStr = detail.getPropContext().split("','");
				String[] tmpStr = tmpDetail.getPropContext().split("','");
				Arrays.sort(detailStr);
				Arrays.sort(tmpStr);
				if(Arrays.equals(detailStr, tmpStr)){
					detail.setId(tmpDetail.getId());
					ids.add(tmpDetail.getId());
					continue;
				}
			}	
			
		}
		//detailService.removeGoodsDetailByGoodsId(goods.getId());
		
		Long[] tmpIds = new Long[ids.size()]; 
		ids.toArray(tmpIds);
		detailService.removeGoodsDetailByNotIds(goods.getId(),tmpIds);
		goodsDao.editGoods(goods);
		for(GoodsProp prop : goods.getProps()){
			prop.setGoods(goods);
			goodsPropService.editGoodsProp(prop);
		}
	}

	@Override
	public void removeGoods(Long[] ids) {
		for(Long id : ids){
			goodsPropService.removeGoodsPropByGoodsId(id);
			detailService.removeGoodsDetailByGoodsId(id);
		}
		goodsDao.removeGoods(ids);
	}

	@Override
	public Goods loadGoods(Long id) {
		Goods goods = goodsDao.loadGoods(id);
		goods.getProps().size();
		goods.getDetails().size();
		return goods;
	}

	@Override
	public Goods loadGoods(Long categoryId, String code) {
		Goods goods = goodsDao.loadGoods(categoryId, code);
		goods.getProps().size();
		goods.getDetails().size();
		return goods;
	}

	@Override
	public Goods loadGoodsByStyleNo(Long categoryId, String styleNo) {
		Goods goods = goodsDao.loadGoodsByStyleNo(categoryId, styleNo);
		goods.getProps().size();
		goods.getDetails().size();
		return goods;
	}

	@Override
	public ResultList<Goods> queryGoods(Long[] categoryIds, String name,
			String pinyin, Long brandId, String branchIds,String styleNo,
			RecordStatus status, OrderBy orderBy, Paginalbe page) {
		return goodsDao.queryGoods(categoryIds, name, pinyin, brandId, branchIds,styleNo, status, orderBy, page);
	}

	public static void main(String[] args) throws IOException{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", "title,fdkfd");
		map.put("name", "name.name!2");
		ObjectMapper mapper = new ObjectMapper();	
		JsonGenerator jsonGenerator = mapper.getJsonFactory().createJsonGenerator(System.out,JsonEncoding.UTF8);
		jsonGenerator.writeObject(map);
		
	}
	
}
